package com.heiyu.api.controller;

import com.google.common.collect.Maps;
import com.heiyu.api.response.PullListDataResponse;
import com.heiyu.api.response.model.PullDataEntity;
import com.heiyu.api.response.model.PullListDataModel;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.GlobalShowInfoMapper;
import com.heiyu.entity.GlobalShowInfo;
import com.heiyu.utils.MoneyUtils;
import com.heiyu.utils.RangeUtil;
import io.swagger.annotations.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api
@RestController
@RequestMapping("/api/v1/pull")
public class PullDataController extends ApiBaseController {


    @Autowired
    private GlobalShowInfoMapper globalShowInfoMapper;

    @ApiOperation(value = "list",notes = "获取数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "targetIds",value = "主键信息逗号分割"),
            @ApiImplicitParam(name = "type",value = "查询数据类型1是货币，2是交易对")
    })
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public PullListDataResponse list(@RequestParam String targetIds, @RequestParam String type){
        PullListDataResponse response= new PullListDataResponse();
        response.success();
        response.setData(new PullListDataModel());
        response.getData().setData(Maps.newHashMap());
        if(type.equals("1")){
            String[] targets = targetIds.split(",");
            GlobalShowInfo showInfo;
            List<GlobalShowInfo> list;
            PullDataEntity entity;
            for(String id:targets){
                showInfo = new GlobalShowInfo();
                showInfo.setTargetId(id);
                list = this.globalShowInfoMapper.selectList(showInfo);
                if(CollectionUtils.isNotEmpty(list)){
                    entity = new PullDataEntity();
                    if(defaultSymbol()){
                        entity.setPrice(MoneyUtils.formatterCoin(list.get(0).getPriceCny()));
                        entity.setExchangePrice(MoneyUtils.formatterCoin(list.get(0).getPriceUsd()));
                        entity.setRange(RangeUtil.formatterNoFlag(list.get(0).getPercentChange24h()));
                        entity.setFullName(list.get(0).getUsName());
                        entity.setTargetId(id);
                        entity.setTradeAmount(MoneyUtils.formatter(list.get(0).getVol24hCny(),true));
                    }else{
                        entity.setPrice(MoneyUtils.formatterCoin(list.get(0).getPriceUsd()));
                        entity.setExchangePrice(MoneyUtils.formatterCoin(list.get(0).getPriceUsd()));
                        entity.setRange(RangeUtil.formatterNoFlag(list.get(0).getPercentChange24h()));
                        entity.setTargetId(id);
                        entity.setFullName(list.get(0).getUsName());
                        entity.setTradeAmount(MoneyUtils.formatter(list.get(0).getVol24hUsd(),true));

                    }
                    response.getData().getData().put(id,entity);
                }
            }
        }
        return response;
    }
}
